package com.stylefeng.guns.rest.common.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Created by tengj on 2017/3/29.
 */
public class MyInterceptor implements HandlerInterceptor {

    // 在执行目标方法之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
;        boolean flag =true;
        String ip = request.getRemoteAddr();
        long startTime = System.currentTimeMillis();
        request.setAttribute("requestStartTime", startTime);
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        System.out.println("用户:"+ip+",访问目标:"+method.getDeclaringClass().getName() + "." + method.getName());

       /* User user=(User)request.getSession().getAttribute("user");
        if(null==user){
            response.sendRedirect("toLogin");
            flag = false;
        }else{
            flag = true;
        }*/


        // 将handler强转为HandlerMethod, 前面已经证实这个handler就是HandlerMethod
        // 从方法处理器中获取出要调用的方法
        // 获取出方法上的Access注解
//        Login login = method.getAnnotation(Login.class);
//        if (login == null) {
            // 如果注解为null, 说明不需要拦截, 直接放过
            /*HttpSession session = request.getSession();
            Admin admin = (Admin) session.getAttribute("admin");
            if(admin == null){
                response.sendRedirect("/admin/login");
            }*/
//            return true;
//        }
//        if (login.isLogin()) {
//
//            String userCode = request.getParameter("userCode");
//            if(!StringUtils.isNotBlank(userCode)){
//                throw new MissingServletRequestParameterException("userCode","String");
//            }
//            String loginToken = request.getParameter("loginToken");
//            if(!StringUtils.isNotBlank(loginToken)){
//                throw new MissingServletRequestParameterException("loginToken","String");
//            }
//            return true;
//        }
        return true;

    }


    // 执行目标方法之后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        long startTime = (Long) request.getAttribute("requestStartTime");
        long endTime = System.currentTimeMillis();
        long executeTime = endTime - startTime;
        // 打印方法执行时间
        if (executeTime > 1000) {
            System.out.println("[" + method.getDeclaringClass().getName() + "." + method.getName() + "] 执行耗时 : "
                    + executeTime + "ms");
        } else {
            System.out.println("[" + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "] 执行耗时 : "
                    + executeTime + "ms");
        }
    }

    // 在请求已经返回之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
